<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../../lib/layui-v2.6.3/css/layui.css" media="all">
    <link rel="stylesheet" href="../../css/public.css" media="all">
    <style>
        .engineer-info {
            font-size: 30px;
            margin: 10px;
        }

        .engineer-img {
            max-width: 300px;
            height: auto;
            margin: 10px;
        }
    </style>
</head>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">

        <fieldset class="table-search-fieldset">
            <legend>搜索信息</legend>
            <div style="margin: 10px 10px 10px 10px">
                <form class="layui-form layui-form-pane" lay-filter="demo" action="">
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">订单状态</label>
                            <div class="layui-input-inline">
                                <select name="orderStatus">

                                    <option value=-1>任何</option>

                                    <option value=0>已提交</option>

                                    <option value=1>已接单</option>

                                    <option value=2>待确认</option>

                                    <option value=3>已完成</option>

                                    <option value=4>未完结</option>

                                </select>
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">优先级</label>
                            <div class="layui-input-inline">
                                <select name="orderPriority">

                                    <option value=-1>任何</option>

                                    <option value=0>正常</option>

                                    <option value=1>较高</option>

                                    <option value=2>高</option>

                                    <option value=3>非常高</option>

                                </select>
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">预约时间</label>
                            <div class="layui-input-inline">
                                <input type="text" name="orderDate" readonly placeholder="任何" class="layui-input demo-table-search-date">
                            </div>
                            <div class="layui-input-inline">
                                <select class="layui-input-inline" name="orderTime">

                                    <option value=-1>任何</option>

                                    <option value=0>上午</option>

                                    <option value=1>下午</option>

                                    <option value=2>晚上</option>

                                </select>
                            </div>
                        </div>

                        <div class="layui-inline">
                            <label class="layui-form-label">手机号</label>
                            <div class="layui-input-inline">
                                <input type="text" name="orderClientphone" class="layui-input">
                            </div>
                        </div>
                        <button type="submit" class="layui-btn layui-btn-primary"  lay-submit lay-filter="data-search-btn"><i class="layui-icon"></i> 搜 索</button>
                    </div>

                </form>

            </div>
        </fieldset>


        <table class="layui-hide" id="currentTableId" lay-filter="currentTableFilter"></table>

        <script type="text/html" id="currentTableBar" >
            {{# if(d.orderStatus === 3){ }}
            <div style="text-align: left;">
                <a class="layui-btn layui-btn-normal layui-bg-green layui-btn-xs data-count-edit" lay-event="engineer">查看</a>
                <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="detail">日志</a>
                <a class="layui-btn layui-btn-normal layui-bg-blue layui-btn-xs" lay-event="viewFlow">查看流水</a>
            </div>

            {{# } else { }}
            <div style="text-align: left;">
                <a class="layui-btn layui-btn-normal layui-bg-green layui-btn-xs data-count-edit" lay-event="engineer">查看</a>
                <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="detail">日志</a>
            </div>

            {{# } }}
        </script>

    </div>
</div>
<script src="../../lib/layui-v2.6.3/layui.js" charset="utf-8"></script>
<script src="../../js/axios.min.js"></script>
<script>

    layui.use(['form', 'table','laydate'], function () {
        var $ = layui.jquery,
            form = layui.form,
            table = layui.table;
            laydate = layui.laydate;

        var originalData;
        axios.defaults.withCredentials = true

        // 自定义验证规则
        form.verify({
            phoneOrEmpty: function(value, item) {
                // 判断是否为空
                if (value === '') {
                    return; // 不进行验证，直接通过
                }
                // 验证手机号格式
                var phoneReg = /^1[34578]\d{9}$/;
                if (!phoneReg.test(value)) {
                    return '请输入正确的手机号格式'; // 返回错误提示信息
                }
            }
        });
        //日期
        laydate.render({
            elem: '.demo-table-search-date'
        });

        axios.get('http://localhost:8080/order/findData', {
            withCredentials: true

        }).then(function(res) {
            originalData = res.data.data;
            table.render({
                elem: '#currentTableId',
                data: originalData,
                page: true,
                limit: 10,
                cols: [[
                    {field: 'productType', width: 110, title: '产品型号',sort: true},
                    {field: 'orderDescription', width: 100, title: '订单描述'},
                    {field: 'orderPriority', width: 80, title: '优先级', templet: '#PriorityIpl'},
                    {field: 'orderStatus', width: 110, title: '订单状态', sort: true, templet: '#StatusIpl'},
                    {field: 'orderClientname', width: 110, title: '用户姓名' },
                    {field: 'orderClientphone', width: 120, title: '用户手机号' },
                    {field: 'orderLocation', width: 160, title: '地区',templet: '#LocationTpl'},
                    {field: 'orderAddress', width: 110, title: '详细地址',},
                    {field: 'orderTime', width: 130, title: '预约时间', templet: '#TimeTpl',sort: true},
                    {title: '操作', minWidth: 150, toolbar: '#currentTableBar', align: "center"},
                ]],

                initSort: {
                    field: 'orderTime',
                    type: 'desc' // 降序排列
                }

            });

        })

        form.on('submit(data-search-btn)', function (data) {

            data = data.field;
            var datafome = new FormData();

            for(var key in data){
                datafome.append(key,data[key]);
            }
            datafome.append("page",1);
            datafome.append("limit",10)
            axios.post('http://localhost:8080/order/searchData', datafome, {
                withCredentials: true
            }).then(function(res) {
                table.reload('currentTableId', {
                    data: res.data.data,
                    page: { // 指定页码和每页显示的数据条数
                        curr: 1,
                        limit: 10
                    },
                    done: function(res, curr, count) {
                        // 重新加载完成后的回调函数
                        console.log('重新加载完成');
                    }
                });
            });
            return false;
        });

        // 监听表格行工具栏事件
        table.on('tool(currentTableFilter)', function(obj){
            var data = obj.data;
            var orderId = String(data.orderId);

            if(obj.event === 'detail'){
                showOrderLog(orderId);
            }
            if(obj.event === 'engineer'){
                showEngineer(data);
            }
            if(obj.event === 'viewFlow'){
                showFlowDetail(orderId);
            }
        });

        function showOrderLog(orderId) {
            layer.open({
                type: 1,
                title: '订单日志',
                area: ['1000px', '600px'],
                content: '<table class="layui-table" id="logTable"></table>',
                success: function(layero, index){
                    // 表格渲染
                    table.render({
                        elem: '#logTable',
                        url: 'http://localhost:8080/order/getLogs/' + orderId,
                        method: 'GET',
                        page: true,
                        limit: 10,
                        cols: [[

                            {field: 'engineerName', title: '工程师',width: 120,},
                            {field: 'logStatus', width:80, title: '状态',templet: '#logStatusTpl'},
                            {title: '操作时间',templet: '#logTimeTpl',width:180},
                            {field: 'logDescription', minWidth:175,title: '描述'},

                        ]]
                    });
                }
            });

        }
        function showFlowDetail(orderId) {
            layer.open({
                type: 1,
                title: '流水明细',
                area: ['800px', '600px'],
                content: '<table class="layui-table" id="flowTable"></table>',

                success: function(layero, index){
                    var form = new FormData();
                    form.append("orderId",orderId);
                    // 发送请求获取流水明细数据
                    axios.post('http://localhost:8080/money/findOne',form, {
                        withCredentials: true,
                    })
                        .then(function (response) {
                            var data = response.data;
                            // 表格渲染
                            table.render({
                                elem: '#flowTable',
                                data: data.data,
                                page: true,
                                limit: 10,
                                cols: [[
                                    {field: 'sysName', title: '用户', width: 120},
                                    {field: 'money', title: '金额', width: 100},
                                    {field: 'logTime', title: '操作时间', width: 180},
                                    {field: 'logDescription', title: '描述'}
                                ]]
                            });
                        })
                        .catch(function (error) {
                            console.log(error);
                        });
                }
            });
        }

        function showEngineer(data) {
            layer.open({
                type: 1,
                title: '工程师信息',
                area: ['1000px', '600px'],
                content: '<div id="engineerContainer"></div>',
                success: function() {
                    var engineerContainer = document.getElementById('engineerContainer');

                    // 显示工程师姓名
                    var engineerName = document.createElement('p');
                    engineerName.textContent = '工程师: ' + data.engineerName;
                    engineerName.classList.add('engineer-info');
                    engineerContainer.appendChild(engineerName);

                    // 显示手机号
                    var engineerPhone = document.createElement('p');
                    engineerPhone.textContent = '手机号: ' + data.engineerPhone;
                    engineerPhone.classList.add('engineer-info');
                    engineerContainer.appendChild(engineerPhone);

                    // 显示接单图片
                    var orderVerify = document.createElement('img');
                    orderVerify.src = 'https://tgu-summer-nine.oss-cn-beijing.aliyuncs.com/orderForm/' + data.orderId + '/' + data.orderVerify;
                    orderVerify.classList.add('engineer-img');
                    engineerContainer.appendChild(orderVerify);
                }
            });
        }


    });

</script>


<!-- 自定义操作工具栏模板 -->

<script type="text/html" id="VerifyTpl">
    <img src="https://tgu-summer-nine.oss-cn-beijing.aliyuncs.com/orderForm/{{ d.orderId }}/{{ d.orderVerify }}" style="min-width: 300px; min-height: 300px;">
</script>



<script type="text/html" id="TimeTpl">
    {{# if(d.orderTime === 0){ }}
    {{d.orderDate}}上午
    {{# } else if(d.orderTime === 1){ }}
    {{d.orderDate}}下午
    {{# } else if(d.orderTime === 2){ }}
    {{d.orderDate}}晚上
    {{# } }}
</script>

<script type="text/html" id="logTimeTpl">
    {{d.logTime.substring(0,10)}} {{d.logTime.substring(11,19)}}
</script>

<script type="text/html" id="LocationTpl">
    {{d.orderProvince}}{{d.orderCity}}{{d.orderArea}}
</script>

<script type="text/html" id="PriorityIpl">
    {{# if(d.orderPriority === 0){ }}
    正常
    {{# } else if(d.orderPriority === 1){ }}
    较高
    {{# } else if(d.orderPriority === 2){ }}
    高
    {{# } else if(d.orderPriority === 3){ }}
    非常高
    {{# } }}
</script>

<script type="text/html" id="StatusIpl">
    {{# if(d.orderStatus === 0){ }}
    已提交
    {{# } else if(d.orderStatus === 1){ }}
    已接单
    {{# } else if(d.orderStatus === 2){ }}
    待确认
    {{# } else if(d.orderStatus === 3){ }}
    已完成
    {{# } else if(d.orderStatus === 4){ }}
    未完结
    {{# } }}
</script>

<script type="text/html" id="logStatusTpl">
    {{# if(d.logStatus === 0){ }}
    已提交
    {{# } else if(d.logStatus === 1){ }}
    已接单
    {{# } else if(d.logStatus === 2){ }}
    待确认
    {{# } else if(d.logStatus === 3){ }}
    已完成
    {{# } else if(d.logStatus === 4){ }}
    未完结
    {{# } }}
</script>

</body>
</html>